﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JinianNet.JNTemplate;
using Microsoft.AspNetCore.Mvc.Formatters.Xml;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ZR.Model.mes.md;
using ZR.Model.mes.md.DTO;
using ZR.Service.mes.md.IService;
using static Org.BouncyCastle.Crypto.Engines.SM2Engine;

namespace ZR.Service.mes.md
{
    [AppService(ServiceType = typeof(IMdProductDefineService), ServiceLifetime = LifeTime.Transient)]
    public class MdProductDefineService : BaseService<MdProductDefine>, IMdProductDefineService
    {
        public int deleteProductDefine(int[] ids)
        {
            return Delete(ids);
        }


        public MdProductDefineDTO GetList(string name, string code, int pageNum, int pageSize)
        {
            int totalNum = 0;
            var predicate = Expressionable.Create<MdProductDefine>()
                .AndIF(name.IsNotEmpty(), pro => pro.ProductName.Contains(name))
                .AndIF(code.IsNotEmpty(), pro => pro.ProductCode.Contains(code)).ToExpression();


            List<MdProductDefine> data = Context.Queryable<MdProductDefine>()
                .LeftJoin<MdUnit>((pro, unit) => pro.ProductUnit == unit.MeasureId)
                .Where(predicate)
                .Select((pro, unit) => new MdProductDefine()
                {
                    ProductUnitName = unit.MeasureName
                },
                true).ToPageList(pageNum, pageSize, ref totalNum);


            MdProductDefineDTO productPageDto = new MdProductDefineDTO();
            productPageDto.list = data;
            productPageDto.Total = totalNum;
            return productPageDto;

        }

        public List<MdProductDefine> GetList(DateTime starttime, DateTime endtime, string productCode, string productName)
        {

            var predicate = Expressionable.Create<MdProductDefine>()
               .AndIF(productCode.IsNotEmpty(), pro => pro.ProductCode.Contains(productCode))
               .AndIF(productName.IsNotEmpty(), pro => pro.ProductName.Contains(productName))
               .AndIF(starttime.IsNotEmpty(), pro => pro.CreatedTime>=starttime)
               .AndIF(endtime.IsNotEmpty(), pro => pro.CreatedTime>=endtime)
               .ToExpression();
           return  Context.Queryable<MdProductDefine>().Where(predicate).ToList();
        }

        public MdProductDefine GetProductDefinebyPK(int measure)
        {
            MdProductDefine  product=GetId(measure);
            MdUnit unit = Context.Queryable<MdUnit>().Where(it=>it.MeasureId == product.ProductUnit).First();
            if (unit != null)
            {
                product.ProductUnitName = unit.MeasureName;
            }
           
            return product;
        }

        public List<MdUnit> GetProductDefineList(string name)
        {
          return  Context.Queryable<MdUnit>().Where(it=>it.MeasureName.Contains(name)).ToList(); 
        }

        public List<MdUnit> GetProductDefineList()
        {
            return Context.Queryable<MdUnit>().ToList();
        }

        
        public List<MdWorkline> GetWorklineList()
        {
            return Context.Queryable<MdWorkline>().ToList();
        }

        public int InsertProductDefine(MdProductDefine jo)
        {
            return Insert(jo);
        }

        public int UpdateProductDefine(MdWorkshop paramss)
        {
            throw new NotImplementedException();
        }

        public int UpdateProductDefine(MdProductDefine paramss)
        {
            return Update(paramss,true);
        }

      


    }
}
